package logic

import (
	"context"
	"errors"
	"real-free/cache"
	"real-free/model"

	"real-free/freerpc/freerpc"
	"real-free/freerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *freerpc.LoginRequest) (*freerpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var user model.User
	model.DB.Debug().Where("username=?", in.Username).Find(&user)
	if user.ID == 0 {
		return nil, errors.New("用户不存在,请更改")
	}
	result, _ := cache.Rdb.Get(l.ctx, "sendSms"+in.Username).Result()
	if result != in.SendSms {
		return nil, errors.New("验证码错误")
	}
	return &freerpc.LoginResponse{}, nil
}
